<?php

class Projects extends Admin_Controller {

	function __construct()
	{
		parent::__construct();	
		
		// build up our header_css array
		$this->aPageCss[] = array(
							'combine_css' => true,
							'base_href' => '/media/css',
							'css' => array('admin_projects'),
							'media' => 'all',
							'version' => '1.0'
		);
		
		$this->load->helper('form');
		$this->load->library('form_validation');
	}
	
	public function index()
	{
		$this->aPageJs[] = array(
							'combine_js' => true,
							'base_href' => '/media/js',
							'js' => array('admin_projects'),
							'version' => '1.0'
		);
		
		$this->load->model('db/baa_projects', 'baa_projects');
		$this->baa_projects->project_status = 'LI';
		$qProjects = $this->baa_projects->get();
		
		$this->aData['qProjects'] = $qProjects;
		
		$this->load->view('platform/'.$this->sPlatform.'/admin/projects/index', $this->aData);
	}
	
	public function add_project()
	{
		$this->edit_project();
	}
	
	public function edit_project($iProjectId = NULL)
	{
		$this->aPageJs[] = array(
							'combine_js' => true,
							'base_href' => '/media/js',
							'js' => array('admin_projects'),
							'version' => '1.0'
		);
		
		$this->load->model('db/views/baa_projects_extended', 'vw_baa_projects_extended');
		
		$oProject = $this->vw_baa_projects_extended->get_object();
		
		if($iProjectId !== NULL)
		{
			$this->vw_baa_projects_extended->project_id = $iProjectId;
			
			$qProject = $this->vw_baa_projects_extended->get(1);
			
			$oProject = $qProject->num_rows() === 1 ? $qProject->row(0) : $oProject;
		}
		
		$this->aData['oProject'] = $oProject;
		
		$this->load->view('platform/'.$this->sPlatform.'/admin/projects/edit', $this->aData);
	}
	
	public function save_project()
	{
		$project_id = $this->input->post('project_id', true, 0);
		$project_title = $this->input->post('project_title', true, '');
		$project_date_month = $this->input->post('project_date_month', true, '');
		$project_date_year = $this->input->post('project_date_year', true, '');
		$project_desc = $this->input->post('project_desc', true, '');
		$project_area = $this->input->post('project_area', true, '');
		
		$project_jpg = $this->input->post('project_jpg', true, NULL);
		$project_pdf = $this->input->post('project_pdf', true, NULL);
		
		$aErrors = array();
		
		if($project_title == '')
		{
			$aErrors[] = 'Please complete the Project title field.';
		}
		if($project_date_month == '')
		{
			$aErrors[] = 'Please complete the Date (month) field.';
		}
		if($project_date_year == '')
		{
			$aErrors[] = 'Please complete the Date (year) field.';
		}
		if($project_desc == '')
		{
			$aErrors[] = 'Please complete the Description field.';
		}
		if(!$project_jpg > 0)
		{
			$aErrors[] = 'Please select a JPG image.';
		}
		
		if(count($aErrors) > 0)
		{
			$aVars = $_POST;
			$aVars['errors'] = '<div class="ep_error">'.implode('</div><div class="ep_error">', $aErrors).'</div>';
			
			$this->load->vars($aVars);
			
			$this->edit_project();
			
			return;
		}
		
		$this->load->model('db/baa_projects', 'baa_projects');
		
		if($project_id > 0)
		{
			$this->baa_projects->project_id = $project_id;
		}
		
		$this->baa_projects->project_title = $project_title;
		$this->baa_projects->project_date_month = $project_date_month;
		$this->baa_projects->project_date_year = $project_date_year;
		$this->baa_projects->project_desc = $project_desc;
		$this->baa_projects->project_area = $project_area;
		$this->baa_projects->project_jpg = $project_jpg;
		$this->baa_projects->project_pdf = $project_pdf;
		$this->baa_projects->project_status = 'LI';
		
		$project_id = $this->baa_projects->save(false);
		
		$message = 'Your Project has been saved';
		$this->load->vars(array('message' => $message));
		
		$this->edit_project($project_id);
	}
	
	private function _get_file_upload_error($error)
	{
		$message = false;
		
		switch($error)
		{
			case 1:
				$message = 'The uploaded file exceeds the maximum script upload size';
				break;
			case 2:
				$message = 'The uploaded file exceeds the maximum form upload size';
				break;
			case 3:
				$message = 'The uploaded file was only partially uploaded';
				break;
			case 4:
				$message = 'No file was uploaded';
				break;
			case 6:
				$message = 'Missing a temporary folder';
				break;
			case 7:
				$message = 'Failed to write file to disk';
				break;
			case 8:
				$message = 'An error occured while uploading the file';
				break;
		}
		
		return $message;
	}
}

?>